﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.VisualBasic;    //使用默认弹出的输入框

namespace KMouse
{
    class Authorize
    {
        public static string[] hardware_info;

        private const ulong keya = 0xfedcba9876543210;

        static Form_Main fm_p;

        static void thread_GetInitHwInfo_Method()
        {
            Dbg.WriteLine("Authorize get init HW info start...");

            hardware_info = Func.GetHarewareInfo(Func.HardwareEnum.Win32_PnPEntity, "Name");

            Dbg.WriteLine("Authorize get init HW info done...");

            thread_GetInitHwInfo.Abort();
        }

        static void thread_GetAuthorizeInfo_Method()
        {
            Dbg.WriteLine("Authorize get authorize info start...");

            string string_cpu = Func.GetCpuID();
            string string_bios = Func.GetBiosID();
            string string_disk = Func.GetDiskID();
            string string_base = Func.GetBaseID();
            string string_video = Func.GetVideoID();
            string string_mac = Func.GetMacID();
            Dbg.WriteLine("Authorize get authorize info done...");
            
            string string_authorize = string_cpu + "\r\n"
            + string_bios + "\r\n"
            + string_disk + "\r\n"
            + string_base + "\r\n"
            + string_video + "\r\n"
            + string_mac + "\r\n";

            byte[] byteArray = System.Text.Encoding.Default.GetBytes(string_authorize);
            ulong authorize_code = 0;            
            for(int i = 0; i < byteArray.Length; i++)                       //每个byte循环累加
            {
                int offset = 8 * (i % 8);
                ulong ss = ((ulong)byteArray[i]) << offset;
                //Console.WriteLine("offset:{0} ss:{1:x}",offset, ss);
                authorize_code += ss;
            }

            ulong authorize_key = authorize_code ^ keya;
            Console.WriteLine("Authorize code:{0:X} key:{1:X} Dict:{2:X}", 
                authorize_code, authorize_key, Param.dict["AUTHORIZE_KEY"]);

            if((string)Param.dict["AUTHORIZE_KEY"] != authorize_key.ToString("X"))         //已经授权过了，不需要再授权了
            {
                fm_p.Invoke((EventHandler)(delegate
                {
                    fm_p.Enabled = false;
                }));

                string key_string = Interaction.InputBox(string_authorize, "Authorize!",
                    "Your code is:" + authorize_code.ToString("X") + ". Plase input your authorize key", -1, -1);

                bool authorize_success = false;
                if(key_string == "kuga is handsome")
                {
                    authorize_success = true;
                }
                else
                {
#if false
                    ulong key_enter = 0;

                    try
                    {
                        key_enter = ulong.Parse(key_string);
                    }
                    catch
                    {
                        key_enter = 0x123456789;
                    }

                    Console.WriteLine("Enter string:{0} key:{1:X}", key_string, key_enter);

                    if(key_enter == authorize_key)
                    {
                        authorize_success = true;
                    }
#else
                    Console.WriteLine("Enter string:{0}", key_string);
                    
                    if(key_string == authorize_key.ToString("X"))
                    {
                        authorize_success = true;
                        Param.dict["AUTHORIZE_KEY"] = key_string;           //保存授权码，以后不需要再输入了
                    }
                    else
                    {
                        //MessageBox.Show("Authorize fail!" +
                        //"Enter:" + key_string + 
                        //"Need:" + authorize_key.ToString("X"), "Authorize");
                    }
#endif
                }

                if(authorize_success == false)
                {
                    MessageBox.Show("Authorize fail, app close!", "Authorize");
                    System.Environment.Exit(0);   //用Close跨线程是关不掉的
                }
                
                MessageBox.Show("Authorize success!", "Authorize");
                fm_p.Invoke((EventHandler)(delegate
                {
                    fm_p.Enabled = true;
                }));

                thread_GetAuthorizeInfo.Abort();
            }
            
        }

        static Thread thread_GetInitHwInfo;
        static Thread thread_GetAuthorizeInfo;

        static public void Get_Init_HW_Info_Start()
        {
            thread_GetInitHwInfo = new Thread(new ThreadStart(thread_GetInitHwInfo_Method));
            thread_GetInitHwInfo.Start();
        }

        static public void Get_Authorize_HW_Info_Start(Form_Main fm_s)
        {
            fm_p = fm_s;

            thread_GetAuthorizeInfo = new Thread(new ThreadStart(thread_GetAuthorizeInfo_Method));
            thread_GetAuthorizeInfo.Start();
        }

        static public void Get_Init_HW_Info_Done()
        {
            while(true)
            {
                if(thread_GetInitHwInfo.IsAlive == false)
                {   
                    break;
                }
                Dbg.WriteLine("Authorize wait:%...", thread_GetInitHwInfo.IsAlive);
                Thread.Sleep(10);
            }            
        }
    }
}
